package com.sbox.util;

import java.io.File;
import java.io.FileOutputStream;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Bitmap.CompressFormat;

public class PictureUtils {
	
	public static Bitmap createPicture(String fileName)
	{
		long len = 0;
		Bitmap bmp = null;
		File f = new File(fileName);
		BitmapFactory.Options options = new BitmapFactory.Options();
		
		if( !f.exists() || f.length() == 0 )
			return null;
			
		len = f.length();
		if( len > AlbumConst.MB*4 )
		{
			options.inSampleSize = 4;		
		}
		else if( len > AlbumConst.KB*100 )
		{			
			options.inSampleSize = 2;	
		}
		
		bmp = BitmapFactory.decodeFile(fileName, options);
		
		return bmp;
	}
	
	public static Bitmap createSmallPicture(String fileName)
	{
		return createSmallPicture(fileName, AlbumConst.PICTURE_SMALL_WIDTH, AlbumConst.PICTURE_SMALL_HEIGHT, AlbumConst.PICTURE_DEFAULT_INSAMPLE);
	}
	
	public static Bitmap createSmallPicture(String fileName, int width, int height, int inSample)
	{
		Bitmap bmp = null;
		BitmapFactory.Options options = new BitmapFactory.Options();
		options.outWidth = width;
		options.outHeight = height;
		options.inSampleSize = inSample;
		
		bmp = BitmapFactory.decodeFile(fileName, options);
		
		return bmp;
	}
	
	public static void savePicture(Bitmap bmp, String fileName) {
		FileOutputStream fos = null;
		
		AlbumLog.info("savePicture: " + fileName);
		try {
			
			fos = new FileOutputStream(fileName);
			bmp.compress(CompressFormat.JPEG, 30, fos);
			fos.close();	
		} catch (Throwable e) {
			AlbumLog.error(e.getMessage());
		}
		
	}
}
